package com.concurrent.chapter1.concurrent05;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;

public class Service {
	private Semaphore semaphore = new Semaphore(3);
	private ReentrantLock lock = new ReentrantLock();
	public void sayHello() {
		try {
			semaphore.acquire();
			System.out.println(Thread.currentThread().getName() + "准备：" + System.currentTimeMillis());
			lock.lock();
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + "打印：" + i);
			}
			System.out.println(Thread.currentThread().getName() + "结束：" + System.currentTimeMillis());
			lock.unlock();
			semaphore.release();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
